package com.digitalbonestudios.slugpug.tv;

import java.util.ArrayList;
import java.util.List;

public class Dog {
    public static final int UP = 0;
    public static final int LEFT = 1;
    public static final int DOWN = 2;
    public static final int RIGHT = 3;
    
    public List<BodyPart> parts = new ArrayList<BodyPart>();
    public int direction;
    public int previousDirection = -1;
    
    public Dog() {        
        direction = UP;
        parts.add(new BodyPart(2, 3));
        parts.add(new BodyPart(2, 4));
        parts.add(new BodyPart(2, 5));
    }
    
    public void turnLeft() {
    	previousDirection = direction;
    	direction = LEFT;
    }
    
    public void turnRight() {
    	previousDirection = direction;
    	direction = RIGHT;
    }
    
    public void turnUp() {
    	previousDirection = direction;
    	direction = UP;
    }
    
    public void turnDown() {
    	previousDirection = direction;
    	direction = DOWN;
    }
    
    public void eat() {
        BodyPart end = parts.get(parts.size()-1); 
        parts.add(new BodyPart(end.x, end.y));
    }
    
    public void advance() {
        BodyPart head = parts.get(0);               
        
        int len = parts.size() - 1;
        for(int i = len; i > 0; i--) {
            BodyPart before = parts.get(i-1);
            BodyPart part = parts.get(i);
            part.x = before.x;
            part.y = before.y;
        }
        
        if(direction == UP)
            head.y -= 1;
        if(direction == LEFT)
            head.x -= 1;
        if(direction == DOWN)
            head.y += 1;
        if(direction == RIGHT)
            head.x += 1;
        
        if(head.x < 0)
            head.x = World.WORLD_WIDTH - 1; //14;
        if(head.x > World.WORLD_WIDTH -1)
            head.x = 0;
        if(head.y < 0)
            head.y = World.WORLD_HEIGHT - 1; //9;
        if(head.y > World.WORLD_HEIGHT - 1)
            head.y = 0;
    }
    
    public boolean checkBitten() {
        int len = parts.size();
        BodyPart head = parts.get(0);
        for(int i = 1; i < len; i++) {
            BodyPart part = parts.get(i);
            if(part.x == head.x && part.y == head.y)
                return true;
        }        
        return false;
    }
    
    public boolean checkSteppedInPoop(int poopX, int poopY) {
    	BodyPart head = parts.get(0);
    	if (head.x == poopX && head.y == poopY) {
    		return true;
    	}
    	return false;
    }
}